// var gulp = require("gulp");


// build要是打包压缩代码的--------

// build中不用watch,connect   多了一个压缩

var gulp = require("gulp");
var concat = require("gulp-concat");
var scripts = require("./conf/index").scripts;
var sass = require('gulp-sass');
sass.compiler = require('node-sass');
var findSync = require("./utils/index").findSync;
var imagemin = require("gulp-imagemin");
var scssjson = require("./conf/index").scss;
var scriptsPathArray = null;
var scriptsTaskAraay = [];
var scssTestarrey = [];
// 引入压缩js的依赖
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;

// 引入css压缩的依赖
let cleanCSS = require('gulp-clean-css');

// 引入编译插件中间量
const babel = require('gulp-babel');
/**
 * @task 转存html  scripts css images
 */
gulp.task("build",function(){
    console.log("hello");
})
gulp.task("html-build", function () {
    gulp.src("./src/*.html").pipe(gulp.dest("./dist"));
})


function mergeMysctipts(json) {
    for (let attr in json) {
        scriptsTaskAraay.push("scripts-build-" + attr);
        gulp.task("scripts-build-" + attr, function () {
            return gulp.src([scripts[attr].src] + "/*.js")
                .pipe(concat(attr + ".js"))
                // 在压缩之前进行编译成es5
                .pipe(babel({
                    presets: ['@babel/env']
                }))
                // 在转存前进行压缩
                .pipe(uglify())
                .pipe(gulp.dest("./dist/scripts"))
                
        })
    }
}
gulp.task('scss-build', function () {
    return gulp.src('./src/scss/index/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('./dist/stylesheets'));
});
function mergeMyscss(json) {
    for (let attr in json) {
        scssTestarrey.push("scss-build-" + attr);
        gulp.task("scss-build-" + attr, function () {
            return gulp.src([json[attr].src] + "/*.scss")
                .pipe(concat(attr + ".scss"))
                .pipe(sass().on('error', sass.logError))
                // 在转存之前，进行css压缩
                .pipe(cleanCSS({debug: true}, (details) => {
                    console.log(`${details.name}: ${details.stats.originalSize}`);
                    console.log(`${details.name}: ${details.stats.minifiedSize}`);
                  }))
                .pipe(gulp.dest("./dist/stylesheets"))
        })
    }
}
function init() {
    scriptsPathArray = findSync("./src/scripts").map(path => {
        return "./" + path.replace(/\\/g, "/");
    })
    let scriptsUnSaveArray = ["!./src/scripts/common/*.js"];
    scriptsPathArray = scriptsPathArray.concat(scriptsUnSaveArray);
    mergeMysctipts(scripts);
    mergeMyscss(scssjson);
}

init();

gulp.task("scss-build", scssTestarrey);
gulp.task("scripts-build", scriptsTaskAraay);
gulp.task("images-build", function () {
    return gulp.src("./src/images/**/*")
     // 图片压缩，开发环境可以不要
        .pipe(imagemin())
        .pipe(gulp.dest("./dist/images"))
})

// 不需要 "watch", "connect"--build是编译阶段
gulp.task("build", ["html", "scripts", "scss", "images"]);

gulp.task("sayhello",function(){
    console.log("sayhello");
})